<?php

class Quotation extends Controller
{
	var $data=array();
	
	public function __construct()
	{
		parent::Controller();
		$this->load->model('Quotation_Model');

		$this->data['warning']="";
		$this->data['other_comment']="";
		$this->data['shipdest_comment']="";
		$this->data['requester_name']="";
//		$this->data['requesting_comp_name']="";
		$this->data['email']="";
		$this->data['your_number']="";
//		$this->data['province_list']=$this->get_province_list();
		$this->data['city_list']=$this->get_city_list();

		$this->data['pickup_time']="";
		$this->data['pickup_time_period']="";
		$this->data['dock_closed_time']="";
		$this->data['dock_closed_time_period']="";
		
		$this->data['pickup_date']="";
		$this->data['pickup_contact_name']="";
		$this->data['pickup_address']="";
		$this->data['pickup_p_code']="";
		$this->data['pickup_fax']="";
		$this->data['pickup_company_name']="";
		$this->data['pickup_phone_number']="";
//		$this->data['pickup_country']="";

		$this->data['destination_contact_name']="";
		$this->data['destination_address']="";
		$this->data['destination_p_code']="";
		$this->data['destination_fax']="";
		$this->data['destination_company_name']="";
		$this->data['destination_phone_number']="";
//		$this->data['destination_country']="";
		
		$this->data['goods_description']="";
		$this->data['total_pieces']="";
		$this->data['total_weight']="";
		$this->data['hand_inst']="";

		$this->data['designator_id']=-1;
		
		// load 'url' helper
		$this->load->helper('url');

		// load 'form' helper
		$this->load->helper('form');

		// load 'validation' class
		$this->load->library('validation');

		// load 'email' class
		$this->load->library('email');
	}
	
	function index()
	{
		if($this->input->post('submit'))
		{
			$this->data['requester_name']=trim($this->input->post('requester_name'));
//			$this->data['requesting_comp_name']=trim($this->input->post('reqesting_company_name'));
			$this->data['email']=trim($this->input->post('email'));
			$this->data['your_number']=trim($this->input->post('your_phone_number'));
			$this->data['pickup_date']=trim($this->input->post('req_pickup_date'));
			$this->data['pickup_contact_name']=trim($this->input->post('pickup_contact_name'));
			$this->data['pickup_address']=trim($this->input->post('pickup_address'));
			$this->data['pickup_p_code']=trim($this->input->post('pickup_p_code'));
			$this->data['pickup_fax']=trim($this->input->post('pickup_fax'));
			$this->data['pickup_company_name']=trim($this->input->post('pickup_company_name'));
			$this->data['pickup_phone_number']=trim($this->input->post('pickup_phone_number'));
	//		$this->data['pickup_country']=trim($this->input->post('pickup_country'));
	
			$this->data['destination_contact_name']=trim($this->input->post('destination_contact_name'));
			$this->data['destination_address']=trim($this->input->post('destination_address'));
			$this->data['destination_p_code']=trim($this->input->post('destination_p_code'));
			$this->data['destination_fax']=trim($this->input->post('destination_fax'));
			$this->data['destination_company_name']=trim($this->input->post('destination_company_name'));
			$this->data['destination_phone_number']=trim($this->input->post('destination_phone_number'));
	//		$this->data['destination_country']=trim($this->input->post('destination_country'));

			$this->data['goods_description']=trim($this->input->post('goods_description'));
			$this->data['total_pieces']=trim($this->input->post('total_pieces'));
			$this->data['total_weight']=trim($this->input->post('total_weight'));
			$this->data['hand_inst']=trim($this->input->post('hand_inst'));
			$this->data['other_comment']=trim($this->input->post('other_comment'));
			$this->data['shipdest_comment']=trim($this->input->post('shipment_destination'));

			if(strcmp("", $this->data['requester_name'])!=0 /* && strcmp("", $this->data['requesting_comp_name'])!=0 */
						&& strcmp("", $this->data['email'])!=0 && strcmp("", $this->data['your_number'])!=0	&& strcmp("", $this->data['pickup_date'])!=0 
						&& strcmp("", $this->data['pickup_contact_name'])!=0 && strcmp("", $this->data['pickup_address'])!=0 && strcmp("", $this->data['pickup_p_code'])!=0 
						&& strcmp("", $this->data['pickup_company_name'])!=0 && strcmp("", $this->data['pickup_phone_number'])!=0 
						&& strcmp("", $this->data['destination_contact_name'])!=0 && strcmp("", $this->data['destination_address'])!=0 
						&& strcmp("", $this->data['destination_p_code'])!=0 && strcmp("", $this->data['destination_p_code'])!=0 
						&& strcmp("", $this->data['destination_company_name'])!=0 && strcmp("", $this->data['destination_phone_number'])!=0
						&& strcmp("", $this->data['goods_description'])!=0 && strcmp("", $this->data['total_pieces'])!=0 && strcmp("", $this->data['total_weight'])!=0
						&& strcmp("", $this->data['hand_inst'])!=0 && strcmp("", $this->data['other_comment'])!=0 && strcmp("", $this->data['shipdest_comment'])!=0)
			{
			
				//date rendered here
					$date = strtok($this->data['pickup_date'], "/");
					$month = strtok("/");
					$year = strtok("/");
					
					$this->data['pickup_date'] = $year."/".$month."/".$date;
			
				//check for email validation
				$this->load->library(array('email', 'form_validation'));
				$this->load->helper(array('email', 'form'));
//				$this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
				$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email|xss_clean');
//				$this->form_validation->set_rules('subject', 'Subject', 'required|xss_clean');
//				$this->form_validation->set_rules('message', 'Message', 'required|xss_clean');

				if($this->form_validation->run() == FALSE)
				{
					$this->data['warning']="Email entered is not valid";
				}
				else
				{
					$this->data['warning']="Your request has been sent!";
					$this->email();
					$this->reset();

/*				
					$name = $this->input->post('name');
					$email = $this->input->post('email');
					$subject = $this->input->post('subject');
					$message = $this->input->post('message');
					$this->email->from($this->data['email'], $this->data['requester_name']);
					$this->email->to('has9live@yahoo.com');
					$this->email->subject("testing....");
					$this->email->message($this->data['other_comment']);
					$this->email->send();
*/
				}			
			
			}
			else if(strcmp("", $this->data['requester_name'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Name of the requester was not entered. <br>";
			}
/*			
			else if(strcmp("", $this->data['requesting_comp_name'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Name of the company requesting was not entered. <br>";
			}
*/
			else if(strcmp("", $this->data['email'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Email was not entered. <br>";
			}
			else if(strcmp("", $this->data['your_number'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Your contact number was not entered. <br>";
			}
			else if(strcmp("", $this->data['pickup_date'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the date you would like to pick up. <br/>";
			}
			else if(strcmp("", $this->data['pickup_contact_name'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the name fo the person who would be picking up. <br/>";
			}
			else if(strcmp("", $this->data['pickup_address'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the address of the destination. <br/>";
			}
			else if(strcmp("", $this->data['pickup_p_code'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the postal/zip code of the destination. <br/>";
			}
			else if(strcmp("", $this->data['pickup_company_name'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the company name of the destination. <br/>";
			}
			else if(strcmp("", $this->data['pickup_phone_number'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Phone number for the destination was not entered.. <br/>";
			}
			else if(strcmp("", $this->data['destination_contact_name'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the name fo the person who would be picking up. <br/>";
			}
			else if(strcmp("", $this->data['destination_address'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the address of the destination. <br/>";
			}
			else if(strcmp("", $this->data['destination_p_code'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the postal/zip code of the destination. <br/>";
			}
			else if(strcmp("", $this->data['destination_company_name'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter the company name of the destination. <br/>";
			}
			else if(strcmp("", $this->data['destination_phone_number'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Phone number for the destination was not entered.. <br/>";
			}
			else if(strcmp("", $this->data['goods_description'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Description of the goods was not entered. <br/>";
			}
			else if(strcmp("", $this->data['total_pieces'])==0 || !is_numeric($this->data['total_pieces']))
			{
				$this->data['warning']=$this->data['warning']."* Total number of pieces was either not entered or is not a number. <br/>";
			}
			else if(strcmp("", $this->data['total_weight'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Total weight was not entered. <br/>";
			}
			else if(strcmp("", $this->data['hand_inst'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Special handling instruction was not entered. <br/>";
			}
			else if(strcmp("", $this->data['other_comment'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter some comment on the shipping. <br/>";
			}
			else if(strcmp("", $this->data['shipdest_comment'])==0)
			{
				$this->data['warning']=$this->data['warning']."* Please enter a comment on the shipping destination. <br/>";
			}
			else
			{
				echo "You may have missed some information!".'<br />';
			}
		}
		else if($this->input->post('reset'))
		{
			$this->reset();
		}

		$this->load->view("quotation",$this->data);
	}
	
	function reset()
	{
		$this->data['requester_name']="";
	//	$this->data['requesting_comp_name']="";
		$this->data['email']="";
		$this->data['your_number']="";

		$this->data['pickup_date']="";
		$this->data['pickup_contact_name']="";
		$this->data['pickup_address']="";
		$this->data['pickup_p_code']="";
		$this->data['pickup_fax']="";
		$this->data['pickup_company_name']="";
		$this->data['pickup_phone_number']="";
		$this->data['goods_description']="";
		$this->data['total_pieces']="";
		$this->data['total_weight']="";
		$this->data['hand_inst']="";
		$this->data['shipdest_comment']="";
		$this->data['other_comment']="";

		$this->data['destination_contact_name']="";
		$this->data['destination_address']="";
		$this->data['destination_p_code']="";
		$this->data['destination_fax']="";
		$this->data['destination_company_name']="";
		$this->data['destination_phone_number']="";

	}
	
	function email()
	{
		$msg="";
		$is_hazardous_id=1;

		$this->load->library('email');

		$this->email->from($this->data['email'], $this->data['requester_name']); //.", ".$this->data['requesting_comp_name']);
		$this->email->to('has_9@hotmail.com');
//		$this->email->to('imoosali@gmail.com');
//		$this->email->cc('has_9@hotmail.com');
//		$this->email->bcc('has9live@yahoo.com');

		$this->email->subject('Inquiry/Quote from Lel-Ltd, Website');

		$is_hazardous=$_POST['is_hazardous'];
		if(isset($is_hazardous))
		{
			if(strcmp($is_hazardous, "h1")==0)
			{
				$is_hazardous_id=1;
			}
			else if(strcmp($is_hazardous, "h1")==0)
			{
				$is_hazardous_id=2;
			}
		}
		if(isset($_POST['requester_designation']))
		{
			if(strcmp($_POST['requester_designation'], "Shipper")==0)
			{
				$this->data['designator_id']=1;
			}
			else if(strcmp($_POST['requester_designation'], "Consignee")==0)
			{
				$this->data['designator_id']=2;
			}
			else if(strcmp($_POST['requester_designation'], "Third_Party")==0)
			{
				$this->data['designator_id']=3;
			}
			
			
			$selected_designator = $_POST['requester_designation'];
			//echo "$selected_designator = ".$selected_designator;
			$msg=$msg."Requester is the         : ".$selected_designator."\n";
		}
		else
		{
			$this->data['warning']=$this->data['warning']."* Designation of requester was not entered. <br>";
		}
		$msg=$msg."Pickup date 				: ".$this->data['pickup_date']."\n";

		if(isset($_POST['pickup_time_period']))
		{
			$this->data['pickup_time_period'] = $_POST['pickup_time_period'];
			$this->data['pickup_time'] = $_POST['pickup_time'];
			$msg=$msg."Ready for pickup at      : ".$this->data['pickup_time']." ".$this->data['pickup_time_period']."\n";
		}
		else
		{
			$this->data['warning']=$this->data['warning']."* Pickup time not entered. <br>";
		}

		if(isset($_POST['dock_closed_time_period']))
		{
			$this->data['dock_closed_time_period'] = $_POST['dock_closed_time_period'];
			$this->data['dock_closed_time'] = $_POST['dock_closed_time'];
			$msg=$msg."Dock closed time		    : ".$this->data['dock_closed_time']." ".$this->data['dock_closed_time_period']."\n";
		}
		else
		{
			$this->data['warning']=$this->data['warning']."* Dock's closing time was not entered. <br>";
		}

		$pickup_city=$this->data['city_list'][$_POST['pickup_city_list']];
//		$pickup_province=$this->data['province_list'][$_POST['pickup_province_list']];
		$msg=$msg."Name        				: ".$this->data['pickup_contact_name']."\n";
//		$msg=$msg."Country (pickup)  		: ".$_POST['pickup_country']."\n";
//		$msg=$msg."Province/State (pickup)  : ".$pickup_province."\n";
//		$msg=$msg."Country (pickup)  		 : ".$this->data['pickup_country']."\n"; 
		$msg=$msg."City (pickup)     	    : ".$pickup_city."\n";
		$msg=$msg."Address (pickup)         : ".$this->data['pickup_address']."\n";
		$msg=$msg."Postal code (pickup)     : ".$this->data['pickup_p_code']."\n";
		$msg=$msg."Fax # (pickup)        	: ".$this->data['pickup_fax']."\n";
		$msg=$msg."Company name # (pickup)  : ".$this->data['pickup_company_name']."\n";
		$msg=$msg."Phone # (pickup)         : ".$this->data['pickup_phone_number']."\n";

		$destination_city=$this->data['city_list'][$_POST['destination_city_list']];
//		$destination_province=$this->data['province_list'][$_POST['destination_province_list']];
		$msg=$msg."Name        				: ".$this->data['destination_contact_name']."\n";
//		$msg=$msg."Country (destination)  		 : ".$this->data['destination_country']."\n"; //$_POST['destination_country']."\n";   
//		$msg=$msg."Province/State (destination)  : ".$destination_province."\n";
		$msg=$msg."City (destination)     	     : ".$destination_city."\n";
		$msg=$msg."Address (destination)         : ".$this->data['destination_address']."\n";
		$msg=$msg."Postal code (destination)     : ".$this->data['destination_p_code']."\n";
		$msg=$msg."Fax # (destination)        	 : ".$this->data['destination_fax']."\n";
		$msg=$msg."Company name # (destination)  : ".$this->data['destination_company_name']."\n";
		$msg=$msg."Phone # (destination)         : ".$this->data['destination_phone_number']."\n";

		$msg=$msg."Hazardous Goods			: ".$is_hazardous."\n";
		$msg=$msg."Goods Description        : ".$this->data['goods_description']."\n";
		$msg=$msg."Number of pieces         : ".$this->data['total_pieces']."\n";

		$this->data['total_weight']=$this->data['total_weight']." ".$_POST['weight_unit'];
		$msg=$msg."Weight         			: ".$this->data['total_weight']."\n";
		$msg=$msg."Special Handle Instruction         : ".$this->data['hand_inst']."\n";
		$msg=$msg."Ship Destination comment  	      : ".$this->data['shipdest_comment']."\n";
		$msg=$msg."Other Comment         	: ".$this->data['other_comment']."\n";

		$this->email->message($msg);
		$this->email->send();


		$origin_address_id=$this->Quotation_Model->add_address($this->data['pickup_contact_name'], $this->data['pickup_company_name'], $this->data['pickup_address'], $this->data['pickup_p_code'], 
																$pickup_city, $this->data['pickup_phone_number'], $this->data['pickup_fax'], " ");

		$destination_address_id=$this->Quotation_Model->add_address($this->data['destination_contact_name'], $this->data['destination_company_name'], $this->data['destination_address'], $this->data['destination_p_code'], 
																$destination_city, $this->data['destination_phone_number'], $this->data['destination_fax'], " ");

		$this->Quotation_Model->add_record(date("Y/m/d"), $this->data['requester_name'], $this->data['email'], $origin_address_id, $destination_address_id,
			$this->data['pickup_date'], $this->data['goods_description'], $this->data['other_comment'], $this->data['shipdest_comment'], 
			$this->data['hand_inst'], $this->data['designator_id'], $this->data['pickup_time']." ".$this->data['pickup_time_period'], 
			$this->data['dock_closed_time']." ".$this->data['dock_closed_time_period'], $this->data['total_weight'],
			$is_hazardous_id, $this->data['your_number'], $this->data['total_pieces']);

//		echo $this->email->print_debugger();
	}
	
	function get_province_list()
	{
		return $this->Quotation_Model->get_province_list();
	}
	function get_city_list()
	{
		return $this->Quotation_Model->get_city_list();
	}

}
?>